// @APIVersion 1.0.0
// @Title mobile API
// @Description mobile has every tool to get any job done, so codename for the new mobile APIs.
// @Contact astaxie@gmail.com
package routers

import (
	"demo/controllers"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/context"
)

func init() {

	//beego 设置访问静态路径
	//beego.SetStaticPath("/static", "static")
	//beego.SetStaticPath("/images", "static/img")
	//beego.SetStaticPath("/css", "static/css")
	//beego.SetStaticPath("/js", "static/js")
	//beego.SetStaticPath("/swagger", "swagger")

	//上传文件列表
	beego.SetStaticPath("/uploads/list", "static/uploads")

	//上传文件页面：http://127.0.0.1:8080/upload/index
	beego.Router("upload/index", &controllers.UploadController{}, "get:Index")
	//上传文件接口
	beego.Router("upload/file", &controllers.UploadController{}, "post:Upload")
	//gorm CURD
	beego.Router("gormQuery/:id", &controllers.OrderController{}, "post:GormQuery")
	//orm CURD
	beego.Router("rawQuery", &controllers.OrderController{}, "post:RawQuery")

	//，http://127.0.0.1:8080/order/updateData/1
	beego.Router("order/Put/:id", &controllers.OrderController{}, "post:Update")
	//，http://127.0.0.1:8080/order/bulkData
	beego.Router("order/bulkData", &controllers.OrderController{}, "post:BulkData")
	//,http://127.0.0.1:8080/order/delete/1
	beego.Router("order/delete/:id", &controllers.OrderController{}, "post:Delete")
	//,http://127.0.0.1:8080/order/get/1
	beego.Router("order/get/:id", &controllers.OrderController{}, "get:GetOne")
	//,http://127.0.0.1:8080/order/add，
	//{"ShopId":1111,"Uid":1000,"Nickname":"张三","Address":"xxxx","InitTime":"2022-12-06 14:38:54"}，
	//{"shop_id":555,"uid":1022,"nickname":"张三11","address":"11xxxx","init_time":"2022-12-06 14:38:54"}
	beego.Router("order/add", &controllers.OrderController{}, "post:Post")

	//protobuf,http://127.0.0.1:8080/protobuf/info
	beego.Router("protobuf/info", &controllers.ProtobufController{}, "get:GetOne")

	//控制器之响应请求,http://127.0.0.1:8080/response/user/getOne
	beego.Router("response/user/getOne", &controllers.ResponseController{}, "get:GetOne")

	//表单数据验证,http://127.0.0.1:8080/validation/user/put?name=111&age=18
	beego.Router("validation/user/put", &controllers.ValidationController{}, "get:Put")

	//路由名字空间（namespace）,一般用来做api版本处理。
	// 创建版本1的名字空间
	ns1 := beego.NewNamespace("/v1",
		// 内嵌一个/user名字空间
		beego.NSNamespace("/user",
			// 下面开始注册路由
			// url路由: /v1/user/info?id=1, http://127.0.0.1:8080/v1/user/info?id=1
			beego.NSRouter("/info", &controllers.UserController{}, "get:GetUserName"),
			// url路由: /v1/user/order
			beego.NSRouter("/getOne/:id", &controllers.Test2Controller{}, "get:GetOne"),
		),
	)
	ns2 := beego.NewNamespace("/v2",
		// 内嵌一个/user名字空间
		beego.NSNamespace("/test2",
			// 下面开始注册路由
			// url路由: /v1/test2/getOne,http://127.0.0.1:8080/v1/user/getOne/1
			beego.NSRouter("/getOne/:id", &controllers.Test2Controller{}, "get:GetOne"),
			//http://127.0.0.1:8080/v2/test2/getAll?query=name:slene&sortby=id&order=desc&offset=0&limit=2
			beego.NSRouter("/getAll", &controllers.Test2Controller{}, "get:GetAll"),
			//PUT：http://127.0.0.1:8080/v2/test2/put/1，raw：{"name":"lisi"}
			beego.NSRouter("/put/:id", &controllers.Test2Controller{}, "put:Put"),
			//http://127.0.0.1:8080/v2/test2/delete/1
			beego.NSRouter("/delete/:id", &controllers.Test2Controller{}, "get:Delete"),
		),
	)

	//注册 namespace
	beego.AddNamespace(ns1)
	beego.AddNamespace(ns2)

	//获取路由参数
	beego.Router("/route/params/:id", &controllers.RouteController{}, "get:GetRouteParams")

	//get
	beego.Get("/alice", func(ctx *context.Context) {
		ctx.Output.Body([]byte("bob"))
	})

	beego.Router("/api/user/userInfo", &controllers.UserController{}, "get:GetUserName")
	beego.Router("/params", &controllers.UserController{}, "get:GetParams")

	beego.Router("/config/list", &controllers.ConfigController{}, "get:GetConfig")
	beego.Router("/", &controllers.MainController{})

	//过滤器
	beego.InsertFilter("/demo/*", beego.BeforeRouter, FilterDemo)

	//注解路由
	//beego.Include(&controllers.UserController{})

	//自动路由
	//beego.AutoRouter(&controllers.UserController{})

	//自定义路由
	beego.Router("/user/list", &controllers.UserController{}, "*:List")
	beego.Router("/user/list", &controllers.UserController{}, "get:List")
	beego.Router("/user/list", &controllers.UserController{}, "post:List")
	beego.Router("/user/list", &controllers.UserController{}, "put:List")
	beego.Router("/user/list", &controllers.UserController{}, "delete:List")
	beego.Router("/user/list", &controllers.UserController{}, "get,post:List")
	beego.Router("/user/list", &controllers.UserController{}, "get:GetList;post:PostList")
	beego.Router("/user/list", &controllers.UserController{}, "*:List;post:PostList")

}

func FilterDemo(ctx *context.Context) {
	title := "页面不存在"
	ctx.WriteString(title)
}
